<h2 class="dialog_title"><span>Rata-rata Perolehan Point Seluruh Departemen</span></h2>
<div class="dialog_content" style="padding: 10px 20px; width:800px">
<table width="100%" border="0" cellpadding="5" cellspacing="0">
	<tr>
		<td>Periode : <strong><?php echo $month ?> - <?php echo $year ?></strong></td>
	</tr>
</table>
<table style="border:1px solid #457A3F;" width="100%" border="0" cellpadding="5" cellspacing="0">
  <tr class="header">
  	<th align="left"><span class="colorWhite">Kode Karyawan</span></th>
  	<th align="left"><span class="colorWhite">Nama Karyawan</span></th>
    <th align="right"><span class="colorWhite">Jumlah Point</span></th>  
  </tr>
  <tbody id="searchKary-table" style="overflow:scroll; height:200px" >
<?php 
	$result = mysql_query(
			"SELECT a.*, b.NAMA_KARYAWAN, e.NAMA_DEPARTMENT, a1.DETACHIEV_ID, 
					f.ACHIEV_JENIS, f.ACHIEV_BIDANG, g.DETACHIEV_TINGKAT, g.DETACHIEV_NAMA,
					a1.DEPERB_COUNT
				FROM perbuatan as a, detil_perbuatan as a1, 
					data_karyawan as b, relasi_div_jab_din as c, 
					dep_divisi_jabatan as d, data_department as e,
					prestasipelanggaran as f, detil_prestasi_pelanggaran as g
				WHERE a.PERB_NO = a1.PERB_NO AND
					a1.DEPERB_STATUS='2' AND 
					a.KODE_KARYAWAN = b.KODE_KARYAWAN AND
					b.KODE_KARYAWAN  = c.KODE_KARYAWAN AND
					c.ID_DEP_DIV_JAB = d.ID_DEP_DIV_JAB AND
					d.ID_DEPARTMENT  = e.ID_DEPARTMENT AND
					f.ACHIEV_ID = g.ACHIEV_ID AND 
					g.DETACHIEV_ID = a1.DETACHIEV_ID AND
					year(a.PERB_TGL)='$year' AND month(a.PERB_TGL)='$month'
			ORDER BY NAMA_DEPARTMENT, NAMA_KARYAWAN, ACHIEV_BIDANG, DEPERB_COUNT");
?>
<?php 
$pointCount = 0;
$nameCount = 0;
$lastName = "";
$data = array();
$deptName = array();
$rankPoint = array();
$deptKaryList = array();

/**
 * kita loop dengan 3 tujuan:
 * 1. Group dengan nama departemen
 * 2. Load ranking karyawan periode sebelumnya
 * 3. Sum point periode saat ini
 */
while ($row = mysql_fetch_assoc($result)){
	//marking nama departemen
	if (!in_array($row['NAMA_DEPARTMENT'], $deptName)) {
		$deptName[] = $row['NAMA_DEPARTMENT'];
	}
	
	//bikin list karyawan yang masuk dalam departemen
	$deptKaryList[$row['NAMA_DEPARTMENT']] = $deptKaryList[$row['NAMA_DEPARTMENT']]? $deptKaryList[$row['NAMA_DEPARTMENT']] : array();
	if (!in_array($row['KODE_KARYAWAN'], $deptKaryList[$row['NAMA_DEPARTMENT']])){
		$deptKaryList[$row['NAMA_DEPARTMENT']][] = $row['KODE_KARYAWAN'];
	}
	
	//@deprecated: ndak hitung pointnya
	//load ranking periode sebelumnya, dan marking
//	if (!array_key_exists($row['KODE_KARYAWAN'], $data)) {
//		$rank = rank_select(false, 
//						"a.KODE_KARYAWAN='".$row['KODE_KARYAWAN']."' 
//						AND MONTH(a.RANK_PERIODE)=$month-1 
//						AND YEAR(a.RANK_PERIODE)='$year'");
//		$rank = mysql_fetch_assoc($rank);
//		$data[$row['KODE_KARYAWAN']]['POINT-1'] = $rank['RANK_POINT']? $rank['RANK_POINT'] : 
//												  $data[$row['KODE_KARYAWAN']]['POINT-1']; 
//	}
	
	//sum Point karyawan
	$data[$row['KODE_KARYAWAN']]['NAMA_KARYAWAN'] = $row['NAMA_KARYAWAN'];
	$data[$row['KODE_KARYAWAN']]['POINT'] += $row['PERB_POINT'];
}	
?>
<?php foreach ($deptName as $dept):?>
    <tr bgcolor="#CCCCCC">
        <td colspan="4"><?php echo $dept ?></td>
    </tr>
    
    <?php foreach ($deptKaryList[$dept] as $key=>$val):?>
    <tr <?php echo tag_zebra($z++)?>>
    	<td><?php echo $val?></td>
    	<td><?php echo $data[$val]['NAMA_KARYAWAN']?></td>
        <td align="right"><?php echo $data[$val]['POINT']? $data[$val]['POINT'] : 0?></td>
    </tr>
    <?php $pointCount += $data[$val]['POINT-1']+$data[$val]['POINT']?>
	<?php endforeach;?>    
<?php endforeach;?>
    
  </tbody>
  <tr class="header">
    <th align="right" colspan="2"><span class="colorWhite">Total Point : </span></th>
    <th align="right"><span class="colorWhite"><?php echo $pointCount?></span></th>  
  </tr>
  <tr class="header">
    <th align="right" colspan="2"><span class="colorWhite">Jumlah Karyawan : </span></th>
    <th align="right"><span class="colorWhite"><?php echo count($data)?></span></th>  
  </tr>
  <tr class="header">
    <th align="right" colspan="2"><span class="colorWhite">Rata-rata Point Seluruh Departemen : </span></th>
    <th align="right"><span class="colorWhite"><?php echo number_format($pointCount/count($data),2) ?></span></th>  
  </tr>
</table>
</div>
<div class="dialog_buttons"><input type="button" value="Close" name="close" onclick="FBModal_hide()" /></div>